﻿using _1_Interfaces.Interfaces.Cjw;
using _1_Interfaces.Models.Dtos.Cjw;
using _1_Interfaces.Models.Tables.Cjw;
using AutoMapper;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;

namespace _2_Services.Services.Cjw
{
    /// <summary>
    /// 聊天服务
    /// </summary>
    public class ChatListService : IChatListService
    {
        SqlSugarClient _db = CjwDbContext.GetInstance();
        private IMapper _mapper;

        public ChatListService(IMapper mapper)
        {
            _mapper = mapper;
        }

        /// <summary>
        /// 小程序发送消息
        /// </summary>
        /// <param name="userSendDto"></param>
        /// <returns></returns>
        public bool UserSendMessage(UserSendDto userSendDto)
        {
            ChatList chatList = _mapper.Map<ChatList>(userSendDto);
            chatList.State = 0;
            chatList.StartTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            chatList.Rate = -1;
            if (_db.Insertable(chatList).ExecuteCommand() == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 医生收取消息
        /// </summary>
        /// <param name="docName">医生帐号</param>
        /// <param name="state">0 或者 1 没有全部 不会传全部的 全部不考虑</param>
        /// <param name="pageIndex">第几页</param>
        /// <param name="pageSize">一页几条</param>
        /// <returns></returns>
        public ChatListDto DocGetMessage(string docName, int state, int pageIndex, int pageSize)
        {
            int totalCount = 0;
            ChatListDto chatListDto = new ChatListDto();
            chatListDto.ChatList = _db.Queryable<ChatList>()
                .Where(x => x.DocName.Equals(docName))
                .Where(x => x.State == state)
                 .ToPageList(pageIndex, pageSize, ref totalCount);
            chatListDto.Total = totalCount;
            chatListDto.TotalPage = Tool.PageCount(totalCount, pageSize);
            return chatListDto;
        }

        /// <summary>
        /// 医生发消息
        /// </summary>
        /// <param name="docSendDto"></param>
        /// <returns></returns>
        public bool DocSendMessage(DocSendDto docSendDto)
        {
            return _db.Updateable<ChatList>()
                .SetColumns(x => x.DocContent == docSendDto.DocContent)
                .SetColumns(x => x.State == 1)
                .SetColumns(x => x.EndTime == DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))
                .Where(x => x.ChatId == docSendDto.ChatId)
                .ExecuteCommandHasChange();
        }

        /// <summary>
        /// 用户看医生回复
        /// </summary>
        /// <param name="openId ">openId</param>
        /// <returns></returns>
        public List<ChatList> UserGetMessage(string openId)
        {
            return _db.Queryable<ChatList>()
                .Where(x => x.OpenId.Equals(openId))
                .Where(x => x.State == 1)
                .ToList();
        }

        /// <summary>
        /// 用户评价
        /// </summary>
        /// <param name="userRateDto"></param>
        /// <returns></returns>
        public bool UserRate(UserRateDto userRateDto)
        {
            return _db.Updateable<ChatList>()
                .SetColumns(x => x.Rate == userRateDto.Rate)
                .Where(x => x.ChatId == userRateDto.ChatId)
                .ExecuteCommandHasChange();
        }
    }
}
